// JavaScript Document
$(document).ready(function(e) {
   $('.featured').masonry({
  		itemSelector: '.fbox'
	});
	
	if($('.fbox').is(':visible')){
		if( $('.fbox').length %2 != 0 ){
			$('.fbox').last().addClass('last');
		}
		
	}
	
	$('.dk').dropkick();
	
	
	
	if($('.page-slider').is(':visible')){
		var slider_nav = '<ul class="slider-nav">';
		var total = $('.page-slider .slide').length;
		
		$('.page-slider .slide:first').addClass('active');
		
		if(total > 1) {
			var ctr = 1;
			$('.page-slider .slide').each(function(index, element) {
				if( $(this).hasClass('active') )
					slider_nav += '<li class="active"><a href="#'+$(this).find('img').attr('alt')+'">'+ ctr++ +'</a></li>';
				else
					slider_nav += '<li><a href="#'+$(this).find('img').attr('alt')+'"">'+ ctr++ +'</a></li>';
			});
			slider_nav += '</ul>';
			$('.slider-text').append(slider_nav);
			
			$(function() {
				setInterval( "slideSwitch()", 5000 );
			});
			
			$('.slider-nav li a').on('click', function(e){
				e.preventDefault();
				
			});
		}
	}
	
	
	
	$('.fcontent').hover(function (e){
		$(this).find('.fmain').fadeIn('fast');
	}, function (){
		$(this).find('.fmain').fadeOut('fast');
	});
	
	$('.site-search-input').click(function (){
		if( $(this).val() == 'Enter Keywords' )
			$(this).val('');
	});
	
	$('.site-search-input').blur(function (){
		if( $(this).val() == '' )
			$(this).val('Enter Keywords')	
	})
	
	
	$('#site_search').submit(function (e){
		if($('.site-search-input').val() == 'Enter Keywords' || $('.site-search-input').val() == '' ) {
			return false;	
		}
		
	})
	
	if( $('.cons-slider').is(':visible') ){
		var slideCount = 0;
		var rem;
		var slides = 5;
		var scrollsize;
		var scrollwidth = 760;
		$('.scroll a').each(function(index, element) {
            slideCount++;
        });
		
		rem = slideCount % slides;
		slideCount = Math.floor(slideCount / slides);
		
		if( rem ){
			scrollsize = (slideCount * scrollwidth) + scrollwidth;
		}
		else {
			scrollsize = slideCount * scrollwidth;
		}
		
		if( scrollsize <= scrollwidth ){
			$('.next').addClass('disabled');
		}
		
		$('.next a').click(function (e){
			e.preventDefault();
			if( !$(this).parent().hasClass('disabled') ){
				$('.scroll').animate({
					'left' : '-=760'
				}, 500, function (){
					var left = $('.scroll').css('width');		
					left = Math.abs(left.replace('px', ''));
					
					if( left >= ( scrollsize - scrollwidth) ){
						$('.next').addClass('disabled');
						if( $('.prev').hasClass('disabled') ){
							$('.prev').removeClass('disabled');
						}
					}	
					
					
				});
			}
		})
		
		
		
		$('.prev a').click(function (e){
			e.preventDefault();
			if( !$(this).parent().hasClass('disabled') ){
				$('.scroll').animate({
					'left' : '+=760'
				}, 500, function (){
					var left = $('.scroll').css('width');		
					left = Math.abs(left.replace('px', ''));
					
					if( left <= ( scrollsize + scrollwidth) ){
						$('.prev').addClass('disabled');
						if( $('.next').hasClass('disabled') ){
							$('.next').removeClass('disabled');
						}
					}	
					
					
				});
			}
		})
		
		
		$('.scroll a').click(function (e){
			e.preventDefault();
			$('.img-slide.active').removeClass('active').hide();
			var target = $(this).attr('href')
			$('.main-image').animate({
				'height' : $(target).height() + 'px'
				}, 500, function (){
					$(target).addClass('active').hide().fadeIn();		
				});
			
			
		});
		
		
		$('.scroll').css('width', scrollsize);
		
				
		$('.img-slide:first').addClass('active');
		var imgWidth = $('.scroll a:first').find('img').attr('src');
	
		
		
		
		
	}
	
	
	$('.lightbox').fancybox({
    	openEffect	: 'elastic',
    	closeEffect	: 'elastic',

    	helpers : {
    		title : {
    			type : 'inside'
    		}
    	}
    });
		
	
	
});

function slideSwitch() {
	
	var $active = $('.page-slider .slide.active');
	var $nav_active = $('.slider-nav li.active');
	
	if ( $active.length == 0 ) $active = $('.page-slider .slide:last');
	if ( $nav_active.length == 0 ) $nav_active = $('.slider-nav li:last');
	
	var $next =  $active.next().length ? $active.next() : $('.page-slider .slide:first');
	var $nav_next =  $nav_active.next().length ? $nav_active.next() : $('.slider-nav li:first');
	
		$active.addClass('last-active');
			
		$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active').css({opacity: 0.0});
		
		$nav_active.addClass('last-active');
		$nav_next.addClass('active')
		$nav_active.removeClass('active last-active');
		
	});
}